package org.yyj.dao.impl;

import org.yyj.dao.DlDao;
import org.yyj.factory.HibernateSessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.yyj.model.Dlb;

public class DlDaoImpl implements DlDao {
    //登录业务：验证用户合法性
    @Override
    public Dlb validate(String xh, String kl) {

            /*//创建Configuration
            Configuration configuration=new
                    Configuration().configure("hibernate.cfg.xml");
            //获取SessionFactory
            SessionFactory sessionFactory=configuration.buildSessionFactory();
            //获取Session
            Session session=sessionFactory.openSession();*/
            Session session= HibernateSessionFactory.getSession();
            //开启事务
            Transaction ts=session.beginTransaction();

            //查询对应学号和口令的学生
            Query query=session.createQuery("from Dlb where xh=? and kl=?");
            //设置参数 setMaxResults: 设置最大返回记录数
            query.setParameter(0,xh);
            query.setParameter(1,kl);
            query.setMaxResults(1);
            Dlb dlb=(Dlb) query.uniqueResult(); //执行查询返回结果
            ts.commit();
            session.close();
            return dlb;
    }
}
